package com.augmentum.oes.controller;

import java.io.IOException;
import java.util.List;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.augmentum.oes.Constants;
import com.augmentum.oes.model.Pagination;
import com.augmentum.oes.model.Question;
import com.augmentum.oes.service.QuestionService;
import com.augmentum.oes.util.SessionUtil;

@Controller
@RequestMapping("/question")
public class QuestionController extends ExceptionHandle {
    @Autowired
    private QuestionService questionService;

    @RequestMapping("/create")
    public String create(Question question) {
        questionService.createQ(question);
        // Tip for save successfully
        SessionUtil.addSession("success", 1);
        return Constants.QUERYQ_PAGE;
    }

    @RequestMapping("/qPage")
    public String qPage() {
        return Constants.QUESTION_PAGE;
    }

    @RequestMapping(value = "/queryJson", produces = "text/html;charset=UTF-8")
    @ResponseBody
     public String queryJson(Pagination<Question> page) throws JsonGenerationException, JsonMappingException, IOException {
     List<Question> list = questionService.dealPage(page);
     page.setItemList(list);
     ObjectMapper om = new ObjectMapper();
     String pageJson = om.writeValueAsString(page);
     return pageJson;
     }

    @RequestMapping("/queryId")
    public ModelAndView queryId(int id) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("question", questionService.getById(id));
        mav.setViewName(Constants.SHOW_QUESTION);
        return mav;
    }

    @RequestMapping("/delete")
    public String delete(int id) {
        questionService.deleteQ(id);
        SessionUtil.addSession("success", 2);
        return Constants.QUERYQ_PAGE;
    }

    @RequestMapping("/batch")
    public String batch(String id) {
        questionService.dealBatch(id);
        SessionUtil.addSession("success", 2);
        return Constants.QUERYQ_PAGE;
    }

    @RequestMapping("/update")
    public String update(Question question) {
        questionService.changeQ(question);
        SessionUtil.addSession("success", 3);
        return Constants.QUERYQ_PAGE;
    }

    @RequestMapping("/updatePage")
    public ModelAndView updatePage(int id) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("type", 2);
        mav.addObject("qtn", questionService.getById(id));
        mav.setViewName(Constants.EDIT_QUESTION);
        return mav;
    }

    @RequestMapping("/createPage")
    public ModelAndView createPage() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("type", 1);
        mav.setViewName(Constants.EDIT_QUESTION);
        return mav;
    }
}
